package com.javainterface.service;

import cn.coremail.apiws.API2;
import cn.coremail.apiws.API2_Service;
import cn.coremail.apiws.ReturnInfo;
import com.javainterface.utils.CoreMailUtil;
import net.sf.json.JSONObject;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;

import java.net.URL;
import java.net.URLEncoder;
import java.util.*;

/**
 * getMailUrl：拼接邮件url链接方法
 * xmlToJson_MailList：将xml转换成json字符串
 * 解析CoreXml获取邮件信息，并调用getMailUrl拼接出url链接，最后转换成json字符串
 */
@Service
public class CoreMailService {

    public String getMailUrl(String appId, String token, String mid, String fid) throws Exception {
        String url = "https://mail.behc.com.cn/apiws/services/API2?wsdl";
        API2 apiPort = (new API2_Service((new URL(url)))).getAPI2Port();
        ReturnInfo ret = apiPort.userLoginEx(token, appId, "face=XT6");
        CoreMailUtil coreMailUtil = new CoreMailUtil();
        if (ret.getCode() == 0) {
            String encodedResult = ret.getResult();
            String sid = coreMailUtil.getParameter(encodedResult, "sid");
            String webname = coreMailUtil.getParameter(encodedResult, "webname");
//            String viewURL = webname + "/coremail";
            String viewURL = "https://mail.behc.com.cn/coremail";
            // 完整页面
//            viewURL += "/XT5/index.jsp?sid=" + sid + "#mail.read|{\"fid\":" + fid + ",\"mid\":\"" + mid + "\",\"mboxa\":\"\"}";
            viewURL += "/XT5/index.jsp?sid=" + sid + "#mail.read|";
            //转义特殊字符
            viewURL+=URLEncoder.encode("{\"fid\":" + fid + ",\"mid\":\"" + mid + "\",\"mboxa\":\"\"}","UTF-8");
            // 独立页面
//              viewURL += "XT5/detach.jsp?sid=" + sid + "#mail.read|{\"fid\":" + fid + ",\"mid\":\"" + mid + "\",\"mboxa\":\"\"}";
            return viewURL;
        } else {
            return "null";
        }

    }

    public JSONObject xmlToJson_MailList(String appId, String token, String xml) throws Exception {
        Document doc = null;
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map> list = new ArrayList<>();
        map.put("code", 200);
        try {
            doc = (Document) DocumentHelper.parseText(xml);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        //指向根节点
        Element root = doc.getRootElement();
        //获取子节点data
        Iterator it_mail = root.elementIterator("mail");
        // 遍历子节点data
        while (it_mail.hasNext()) {
            Map<String, String> map_child = new HashMap<String, String>();
            Element element_mail = (Element) it_mail.next();
            map_child.put("subject", element_mail.elementTextTrim("subject"));
            map_child.put("date", element_mail.elementTextTrim("date"));
            String jumpLink=getMailUrl(appId, token, element_mail.elementTextTrim("mid"), element_mail.elementTextTrim("fid"));
            map_child.put("jumpLink", jumpLink);
            list.add(map_child);
        }
        map.put("data", list);
        JSONObject jsonObject = JSONObject.fromObject(map);
        return jsonObject;
    }

}
